﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Album
{
    public partial class AlbumEdit : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            int albumID = 0;//暂用相册ID，暂用暂用暂用暂用暂用暂用暂用暂用暂用暂用暂用暂用暂用暂用
            string strAlbumID = Request.QueryString["AlbumID"];

            string Cmd = Request.QueryString["Cmd"];
            
            if (Cmd == "del")//删除
            {
                //这里要更新照片的相册为默认相册后才删除该相册.
                new BLL.User_Album().Delete(Convert.ToInt32(strAlbumID));
                Response.Redirect("/Album/MyAlbum.aspx");
            }
          
            if (!string.IsNullOrEmpty(strAlbumID))
            {
                albumID = Convert.ToInt32(strAlbumID);
                //hidAlbumID =Convert.ToInt32(strAlbumID);
            }
            else
            {
                strAlbumID = Request.Form["hidAlbumID"];
                albumID = int.Parse(strAlbumID);
            }
            
            
            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_albumedit"].ToString());//取得主HTML页面
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());
            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion
            //编辑相册部分            
            Jy123.Model.User_Album mAlbum = new Jy123.Model.User_Album();
            Jy123.BLL.User_Album bllAlbum = new Jy123.BLL.User_Album();
            mAlbum = bllAlbum.GetModel(albumID);

            string strSaveAlbum = Request.Form["saveAlbum"];
            string msg = string.Empty;
            if (strSaveAlbum == "保存相册")
            {
                mAlbum.Title = Request.Form["albumName"];
                mAlbum.Description = Request.Form["albumDescription"];
                mAlbum.Privacy =Convert.ToInt32(Request.Form["selPrivacy"]);
                mAlbum.LastUploadTime = DateTime.Now;
                bllAlbum.Update(mAlbum);

                msg = "<script>$(function(){$.moonbox.ok('保存相册成功!');})</script>";
            }
                strHtml = strHtml.Replace("$ImgCount$",mAlbum.ImagesCount.ToString());
                strHtml = strHtml.Replace("$albumName$", mAlbum.Title);
                strHtml = strHtml.Replace("$albumDescription$", mAlbum.Description);
                strHtml = strHtml.Replace("$AlbumID$",albumID.ToString());

                //隐私下拉表的绑定
                switch (mAlbum.Privacy)
                {
                    case 0: strHtml = strHtml.Replace("$id0$","selected")
                        .Replace("$id1$","")
                        .Replace("$id2$","")
                        .Replace("$id3$","");
                        break;
                    case 1:strHtml = strHtml.Replace("$id0$","")
                        .Replace("$id1$","selected")
                        .Replace("$id2$","")
                        .Replace("$id3$","");
                        break;
                    case 2:strHtml = strHtml.Replace("$id0$","")
                        .Replace("$id1$","")
                        .Replace("$id2$","selected")
                        .Replace("$id3$","");
                        break;
                    case 3: strHtml = strHtml.Replace("$id0$", "")
                     .Replace("$id1$", "")
                     .Replace("$id2$", "")
                     .Replace("$id3$", "selected");
                        break;
                }

            Response.Write(strHtml.ToString()+msg);

            
        }


    }
}
